package com.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.hronline.EmployeeDetailAdapterDelegate;
import com.hronline.R;
import com.models.AbsenceSummary;
import com.models.EmergencyContact;
import com.models.Employees;

public class EmployeeDetailAdapter extends BaseAdapter {
	
	public final int ABSENCE_TAB		=	1;
	public final int INFORMATION_TAB	=	2;
	public final int EMERGENCY_TAB		=	3;
	
	private EmployeeDetailAdapterDelegate delegate;
	private Context context;
	private LayoutInflater inflater;
	private int tabToDisplay;
	
	public AbsenceSummary absenceSummary;
	public ArrayList<EmergencyContact> arrEmergency;
	public Employees empInfo;
	public String adviceNumber;
	private SimpleDateFormat stringToDateFormater;
	private SimpleDateFormat dateToStringFormater;
	private OnClickListener callNumberListener;
	
	
	public EmployeeDetailAdapter(Context context, final EmployeeDetailAdapterDelegate delegate){
		super();
		this.context	=	context;
		this.inflater	=	(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.delegate	=	delegate;
		stringToDateFormater		=	new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		dateToStringFormater		=	new SimpleDateFormat("dd MMMM yyyy");
		
		callNumberListener	=	new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TextView	txtTemp	=	(TextView)v;
				delegate.callTheNumber(txtTemp.getText().toString());
			}
		};
	}
	
	public void setTabToDisplay(int displayTab){
		tabToDisplay	=	displayTab;
	}
	
	public void setAbsenceSummary(AbsenceSummary absenceSummary){
		this.absenceSummary	=	absenceSummary;
	}
	
	public void setEmployeeInfo(Employees empInfo){
		this.empInfo	=	empInfo;
	}
	
	public void setEmergencyList(ArrayList<EmergencyContact> arrEmergency){
		this.arrEmergency	=	arrEmergency;
	}
	
	public void setAdviceNumber(String adviceNumber){
		this.adviceNumber	=	adviceNumber;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		switch (tabToDisplay) {
		case ABSENCE_TAB:
			return 2;
			
		case INFORMATION_TAB:
			return 1;

		default:
			if(arrEmergency != null){
				if(arrEmergency.size() == 0){
					return 1;
				}
				else if (arrEmergency.size() > 2){
					return 2;
				}
				else{
					return arrEmergency.size();
				}
			}
			else{
				return 1;
			}
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		switch (tabToDisplay) {
			case ABSENCE_TAB:
				return createViewForAbsenceTab(convertView, position);
				
			case INFORMATION_TAB:
				return createViewForInfromationTab(convertView, position);
	
			default: //EMERGENCY_TAB
				return createViewForEmergencyTab(convertView, position, parent);
		}
	}
	
	/* this method is used to create view for absence tab */
	View createViewForAbsenceTab(View convertView, int position){
		View row	=	convertView;
		
		if(position == 0){
			row	=	this.inflater.inflate(R.layout.absence_summary_view, null);
			
			/* display data */
			TextView txtEmpName				=	(TextView)row.findViewById(R.id.txtEmployeeName);
			TextView txtAbsenceDayRemain	=	(TextView)row.findViewById(R.id.txtAbsenceDayRemain);
			
			if(absenceSummary != null){
				txtEmpName.setText(absenceSummary.getEmployeeName());
				
				String strRemaining;
				
				float remainingFloat	=	Float.parseFloat(absenceSummary.getAllowanceRemaining());
				if(remainingFloat % 1 != 0){
					strRemaining	=	String.format("%f", remainingFloat);
				}
				else{
					strRemaining	=	String.format("%d", (int)remainingFloat);
				}
				
				txtAbsenceDayRemain.setText(strRemaining);
			}
		}
		else{
			row	=	this.inflater.inflate(R.layout.employee_details_request_absence, null);
			
			View requestAbsenceRow	=	row.findViewById(R.id.layout_request_absence_employee_detail);
			
			TextView txtBadgeNumber	=	(TextView)row.findViewById(R.id.txtBagdeNumber);
			txtBadgeNumber.setVisibility(View.INVISIBLE);
			
			if(absenceSummary!=null){
				if(Float.parseFloat(absenceSummary.getAwaitingApproval()) != 0){
					txtBadgeNumber.setVisibility(View.VISIBLE);
					txtBadgeNumber.setText(absenceSummary.getAwaitingApproval());
				}
			}
			
			requestAbsenceRow.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					delegate.didClickRequestAbsence();
				}
			});
			
			View approveAbsenceRow	=	row.findViewById(R.id.layout_approve_absence_employee_detail);
			approveAbsenceRow.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					delegate.didClickApproveAbsence();
				}
			});
			
			
			View logSicknessRow	=	row.findViewById(R.id.layout_log_sickness_employee_detail);
			logSicknessRow.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					delegate.didClickLogSickness();
				}
			});
			
			View logLatenessRow	=	row.findViewById(R.id.layout_log_lateness_employee_detail);
			logLatenessRow.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					delegate.didClickLogLateness();
				}
			});
		}
		
		return row;
	}
	
	/* this method is used to create view for information tab */
	View createViewForInfromationTab(View convertView, int position){
		
		View row	=	convertView;
		
		row			=	this.inflater.inflate(R.layout.employee_detail_information_view, null);
		
		TextView txtEmpName		=	(TextView)row.findViewById(R.id.txtEmployeeName);
		TextView txtJobTitle	=	(TextView)row.findViewById(R.id.txtJobTitle);
		TextView txtStartDate	=	(TextView)row.findViewById(R.id.txtStartedValue);
		TextView txtTelephone	=	(TextView)row.findViewById(R.id.txtTelephoneValue);
		TextView txtMobile		=	(TextView)row.findViewById(R.id.txtMobileValue);
		TextView txtEmail		=	(TextView)row.findViewById(R.id.txtEmailValue);
		Button   btnCallAdvice	=	(Button)row.findViewById(R.id.btnCallAdvice);
		
		if(empInfo != null){
			txtEmpName.setText(String.format("%s %s", empInfo.surName, empInfo.foreName));
			txtJobTitle.setText(empInfo.jobTitle);
			
			try {
				Date date = stringToDateFormater.parse(empInfo.getStartDate());
				txtStartDate.setText(dateToStringFormater.format(date));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			txtTelephone.setText(empInfo.getHomeNumber());
			txtMobile.setText(empInfo.getMobileNumber());
			txtEmail.setText(empInfo.getEmail());
			
			txtTelephone.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
			txtMobile.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
			txtEmail.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
			
			txtTelephone.setOnClickListener(callNumberListener);
			txtMobile.setOnClickListener(callNumberListener);
			txtEmail.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					TextView txtTemp	=	(TextView)v;
					delegate.sendEmail(txtTemp.getText().toString());
				}
			});
			
			btnCallAdvice.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					delegate.callTheNumber(adviceNumber);
				}
			});
		}
		
		return row;
	}
	
	/* this method is used to create view for emergency tab */
	View createViewForEmergencyTab(View convertView, int position, ViewGroup parent){
		View emergencyRow = convertView;
		
		if(arrEmergency != null){
			if(arrEmergency.size() == 0){
				emergencyRow	=	this.inflater.inflate(R.layout.no_emergency_layout, parent, false);
			}
			else{
				emergencyRow	=	this.inflater.inflate(R.layout.employee_detail_emergency_row, null);
				
				TextView txtRelationName	=	(TextView)emergencyRow.findViewById(R.id.txtRelationName);
				TextView txtRelationship	=	(TextView)emergencyRow.findViewById(R.id.txtRelationship);
				TextView txtWorkNumber		=	(TextView)emergencyRow.findViewById(R.id.txtWorkNumberValue);
				TextView txtHomeNumber		=	(TextView)emergencyRow.findViewById(R.id.txtHomeNumberValue);
				TextView txtMobileNumber	=	(TextView)emergencyRow.findViewById(R.id.txtMobileNumberValue);
				
				EmergencyContact contact	=	arrEmergency.get(position);
				
				txtRelationName.setText(contact.getName());
				txtRelationship.setText(contact.getRelationship());
				txtWorkNumber.setText(contact.getWorkPhone());
				txtHomeNumber.setText(contact.getHomePhone());
				txtMobileNumber.setText(contact.getMobilePhone());
				
				txtWorkNumber.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
				txtHomeNumber.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
				txtMobileNumber.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
				
				txtWorkNumber.setOnClickListener(callNumberListener);
				txtHomeNumber.setOnClickListener(callNumberListener);
				txtMobileNumber.setOnClickListener(callNumberListener);
			}
		}
		
		return emergencyRow;
	}
}
